<?php
	require_once("includes/init.inc.php");
	require_once("includes/api.tags.php");
	require_once("includes/api.files.php");

	mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWD) or die(mysql_error());
	mysql_select_db(DB_NAME) or die(mysql_error());
	
	$errNew = "";
	$err = "";
	
	//Nouveau tag
	if(isset($_POST['new']) && $_POST['new'] == 1){
		if($_POST['name'] != ""){
			if(!insert_new_tag($_POST['name'])){
				$errNew = "Please input the name of the new tag";
			}
		}else{
			$errNew = "Please input the name of the new tag";
		}
	}
	
	//Modif tag
	if(isset($_POST['update']) && $_POST['update'] == 1){
		foreach($_POST as $key => $value){
			if($key != "update"){
				update_tag_by_id($key,$value);
			}
		}
	}
	
	//Del tag
	if(isset($_POST['idDel']) && $_POST['idDel'] > 0){
		if(!delete_tag($_POST['idDel'])){
			$err = "The tag could not be deleted, please try again later.";
		}
	}
	
	include("includes/header.inc.php");
?>
				<td id="contenu" style="height:300px;">
					<!-- Contenu commence ici -->
                    <form name="addTag" method="post" action=""><p>
                    	<?php echo(($errNew != "") ? '<span style="color:red">'.$errNew.'</span><br>':'')?>
                    	<input type="text" name="name" /><input type="submit" value="Add a new tag" /><input type="hidden" name="new" value="1" />
                    </p></form>
					<table border="0" cellpadding="0" cellspacing="0">
						<tr>
							<td width="40%" class="title">Tags</td>
							<td></td>
						</tr>
                        <?php echo (($err != "") ? '<tr><td style="color:red;">'.$err.'</td></tr>':'')?>
						<tr>
							<td width="60%"><form name="tags" method="post" action=""><input type="hidden" name="update" value="1" /><?php
                                $tabTag = get_all_tags_for_current_user(false);
								foreach($tabTag as $row){
									echo '<input type="text" name="'.$row["id"].'" value="'.$row["name"].'" /><a onClick="document.getElementById(\'idDel\').value='.$row["id"].'; document.del.submit();" class="delete">X</span></a><br />';
								}
								echo '<br /><br /><input type="submit" value="Save" />';
							?></form></td>
							<td></td>
						</tr>
					</table>
                    <form name="del" method="post" action="">
                    	<input type="hidden" id="idDel" name="idDel" value="0" />
                    </form>

					<!-- Contenu fini ici -->
				</td>
<?php
	include("includes/footer.inc.php");
?>
